//
//  OAuthViewController.swift
//  weibo
//
//  Created by 孙新 on 16/4/19.
//  Copyright © 2016年 孙新. All rights reserved.
//

import UIKit
import SVProgressHUD



class OAuthViewController: UIViewController {
    
    //APPKEY 与 回调页放在公开的地方方便别的地方使用
    private let APP_KEY = "3449423477"
    private let REDRECT_URT = "http://www.baidu.com/"
    private let APP_SERECT = "380200e2ded01753a3c071c207d99aaf"
    
    override func loadView() {
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
         setupUI()  
    }
    
    //加载视图
    private func setupUI(){
        
        //设置控制器的背景颜色
        view.backgroundColor = UIColor.whiteColor()
        //添加导航栏按钮
        self.title = "登 录"
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "autoFill")
    }
    
    //点击导航栏关闭按钮
   @objc private func close(){
        //dismiss掉当前控制器
    dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    //点击导航栏自动填充按钮
   @objc private func autoFill(){
        let jsString = "document.getElementById('userId').value='15098204391';document.getElementById('passwd').value='220481007'"
    webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    
    //MARK - 懒加载webView
    private lazy var webView:UIWebView = {
        let webView = UIWebView()
        webView.delegate = self
        return webView
    }()


}
extension OAuthViewController:UIWebViewDelegate{
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        return true
    }
}

